package GuessNumberGUI;

import Common.Game;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Main extends JFrame implements Game{
    int chances;
    int guess;
    int answer;

    JButton resetBtn;
    JButton guessBtn;
    JTextField jtf;

    class EnterKeyAdapter implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
            if((char)e.getKeyChar() == KeyEvent.VK_ENTER) {
                checkGuess();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {

        }

        @Override
        public void keyReleased(KeyEvent e) {

        }
    }

    public void initData(){
        this.chances = 7;
        this.answer = (int)(Math.random()*100 + 1);
    }

    public void resetGame(){
        this.initData();
        this.jtf.setText("");
        this.guessBtn.setEnabled(true);
    }

    public void checkGuess() {
        try{
            this.guess = Integer.parseInt(this.jtf.getText());
            this.chances -= 1;
            if (this.chances <= 0){
                JOptionPane.showMessageDialog(this,
                        "啊哦！没有机会咯！","提示",
                        JOptionPane.INFORMATION_MESSAGE);
                this.guessBtn.setEnabled(false);
                this.resetBtn.setEnabled(true);
            }else if((this.guess < this.answer)){
                JOptionPane.showMessageDialog(this,
                        "小了！还有"+this.chances+"次机会！",
                        "提示", JOptionPane.INFORMATION_MESSAGE);
            }else if(this.guess > this.answer){
                JOptionPane.showMessageDialog(this,
                        "大了！还有"+this.chances+"次机会！",
                        "提示", JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this,
                        "恭喜！你猜对啦！",
                        "提示", JOptionPane.INFORMATION_MESSAGE);
                this.guessBtn.setEnabled(false);
            }
        }catch (Exception e){
            JOptionPane.showMessageDialog(this,
                    "请输入数字", "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    public void run(){
        this.initData();
        setTitle("猜数字");
        setLocation(400, 100);
        setSize(300, 200);
        Container container = getContentPane();

        JPanel panel = new JPanel();
        panel.setLayout(null);

        JLabel description = new JLabel();
        description.setText("请猜一个1-100之间的整数");
        description.setBounds(70, 10, 150, 30);
        panel.add(description);

        jtf = new JTextField();
        jtf.setBounds(60, 50, 180, 30);
        jtf.addKeyListener(new EnterKeyAdapter());
        panel.add(jtf);

        guessBtn = new JButton("猜一下");
        guessBtn.setBounds(60, 90, 85, 30);
        guessBtn.addActionListener(event -> {this.checkGuess();});
        panel.add(guessBtn);

        resetBtn = new JButton("重新开始");
        resetBtn.setBounds(150, 90, 90, 30);
        resetBtn.addActionListener(e -> {this.resetGame();});
        panel.add(resetBtn);

        container.add(panel);
        setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        Main m = new Main();
        m.run();
    }
}
